package com.symone.webcms.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.symone.webcms.controller.base.BaseController;
import com.symone.webcms.model.LoginLog;
import com.symone.webcms.model.base.ResultMessage;
import com.symone.webcms.service.LoginLogService;
import com.symone.webcms.util.DateUtil;
import com.xiaoleilu.hutool.lang.Validator;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 *   @description : LoginLog 管理
 *   ---------------------------------
 * 	 @author Archer Chu
 *   @since 2018-11-18
 */
@RestController
@Api(value="/loginLog", description="登录日志管理")
@RequestMapping("api/loginLog")
public class LoginLogController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(LoginLogController.class);

    @Autowired
    public LoginLogService loginLogService;

    /**
     * @description : 获取分页列表
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2018-11-18
     */
    @RequiresPermissions("chart:loginLog:index")
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public ResultMessage page(String month, String loginName, Integer pageSize, Integer pageNum) {
        Wrapper<LoginLog> ew = new EntityWrapper<LoginLog>();
        ew.where("deleted=0");

        if(Validator.isEmpty(month)) {
            ew.and().where("create_time>{0}", DateUtil.getMonthFirstDay(new Date()));
            ew.and().where("create_time<{0}", DateUtil.getMonthLastDay(new Date()));
        } else {
            ew.and().where("DATE_FORMAT( create_time, '%Y-%m' )={0}",month);
        }

        if( false == Validator.isEmpty(loginName)) {
            ew.and().like("login_name",loginName);
        }
        ew.orderBy("create_time",false);
        //logger.debug("SQL:"+ew.originalSql());

        if(pageNum == null){
            pageNum = 1;
        }

        if(pageSize == null){
            pageSize = 10;
        }

        Page<LoginLog> page = new Page<LoginLog>(Integer.valueOf(pageNum),Integer.valueOf(pageSize));
        page = loginLogService.findPage(page, ew);

        return ResultMessage.success("获取Log分页列表",page.getRecords(),page.getTotal());
    }

    /**
     * @description : 通过id逻辑删除LoginLog
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2018-11-18
     */
    @RequiresPermissions("chart:loginLog:remove_flag")
    @RequestMapping(value = "/removeFlag", method = RequestMethod.PUT)
    public ResultMessage removeFlag(Long id){
        if(loginLogService.deleteFlagById(id)) {
            return ResultMessage.success("逻辑LoginLog删除成功！");
        } else {
            return ResultMessage.fail("逻辑LoginLog删除失败！");
        }
    }

    /**
     * @description : 通过id逻辑删除LoginLog
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2018-11-18
     */
    @RequiresPermissions("chart:loginLog:remove")
    @RequestMapping(value = "/remove", method = RequestMethod.DELETE)
    public ResultMessage remove(long id){
        if(loginLogService.deleteById(id)) {
            return ResultMessage.success("物理删除LoginLog成功！");
        } else {
            return ResultMessage.fail("物理删除LoginLog失败！");
        }
    }

    /**
     * @description : 清除登录日志记录
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2018-11-18
     */
    @RequiresPermissions("chart:loginLog:remove")
    @RequestMapping(value = "/clear", method = RequestMethod.DELETE)
    public ResultMessage clear(){
        int num = loginLogService.deleteAll();
        if(num > 0) {
            return ResultMessage.success("已经清理了 "+ num +" 条记录！");
        } else {
            return ResultMessage.fail("没有日志记录被清除！");
        }
    }

}
